/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import MiniProjet.gestionnaires.GestionnaireFilms;
import MiniProjet.gestionnaires.GestionnaireGenres;
import MiniProjet.gestionnaires.GestionnaireRubriques;
import MiniProjet.gestionnaires.GestionnaireUtilisateurs;
import MiniProjet.modeles.Utilisateur;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author SaLaH
 */
@WebServlet(name = "ServletUsers", urlPatterns = {"/ServletUsers"})
public class ServletUsers extends HttpServlet {
    @EJB
    private GestionnaireUtilisateurs gestionnaireUtilisateurs;



    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


         String action = request.getParameter("action");
            if (request.getParameter("action") != null) {

             if(action.equals("creerUnUtilisateur")){
                /* données recu du forumlaire */
                String prenomRecu = request.getParameter("prenom");
                String nomRecu = request.getParameter("nom");
                String emailRecu = request.getParameter("email");
                String mdpRecu = request.getParameter("password");
                String verif_mdp = request.getParameter("verif_password");
                boolean estAdmin = false;
                String recup_bool_amin = request.getParameter("choixadmin");


                if(recup_bool_amin.equals("oui")){
                    estAdmin= true;
                }
                else if(recup_bool_amin.equals("non"))
                        {estAdmin =false;}
                  //Vefification de l'existance du cleint dans la BDD

                    Utilisateur clientDEverif = gestionnaireUtilisateurs.getUtilisateurByEmail(emailRecu);
                    if(clientDEverif != null){
                        request.setAttribute("ClientExisteDeja", "L'adresse mail de ce client existe d&eacute;ja");
                        request.setAttribute("recup_nom", nomRecu);
                         request.setAttribute("recup_prenom", prenomRecu);
                          request.setAttribute("recup_email", emailRecu);
                    }else
                    {
                        if(nomRecu.equals("") || prenomRecu.equals("") || emailRecu.equals("") || mdpRecu.equals("") || verif_mdp.equals("")){
                            request.setAttribute("qquchampsvides", "Veuillez remplir tous les champs! ");
                            request.setAttribute("recup_nom", nomRecu);
                            request.setAttribute("recup_prenom", prenomRecu);
                            request.setAttribute("recup_email", emailRecu);
                        }else {
                            if(!mdpRecu.equals(verif_mdp)){
                                    request.setAttribute("mdp_diff", "Le mot passe de v&eacute;rification ne correspond pas!");
                                    request.setAttribute("recup_nom", nomRecu);
                                    request.setAttribute("recup_prenom", prenomRecu);
                                    request.setAttribute("recup_email", emailRecu);
                            }else{
                                gestionnaireUtilisateurs.creeUtilisateur(nomRecu, prenomRecu, emailRecu, mdpRecu,estAdmin);
                                request.setAttribute("creaClientOK", "Nouveau client CinéShop crée");
                                }
                            }
              }





            }
              if(action.equals("afficherUtilisateurs")){
                 Collection<Utilisateur> liste = gestionnaireUtilisateurs.getAllUsers();
                request.setAttribute("listeU", liste);

             }

 }

        RequestDispatcher dp = request.getRequestDispatcher("addClient.jsp");
        dp.forward(request, response);
        // Après un forward, plus rien ne peut être exécuté après !



    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
